//WindZ阅
package com.boda.xy;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * StudentRecord class represents a student's academic record.
 */
class StudentRecord {
    String name;
    String id;
    String course;
    int score;

    /**
     * Constructs a new StudentRecord.
     *
     * @param name   student's name
     * @param id     student's ID
     * @param course course name
     * @param score  student's score
     */
    public StudentRecord(String name, String id, String course, int score) {
        this.name = name;
        this.id = id;
        this.course = course;
        this.score = score;
    }
}

/**
 * GradeManagementSystem class provides functionalities to manage student grades.
 */
public class GradeManagementSystem {
    private static final List<StudentRecord> records = new ArrayList<>();
    private static final Scanner scanner = new Scanner(System.in);

    /**
     * Main method to run the grade management system.
     *
     * @param args command line arguments
     */
    public static void main(String[] args) {
        while (true) {
            displayMenu();
            int choice = getIntInput("请输入选项序号：");
            switch (choice) {
                case 1: addRecord(); break;
                case 2: queryRecord(); break;
                case 3: calculateStats(); break;
                case 4: System.out.println("感谢使用学生成绩管理系统，再见！"); return;
                default: System.out.println("Invalid choice. Please try again.");
            }
        }
    }

    /**
     * Displays the main menu of the system.
     */
    private static void displayMenu() {
        System.out.println("\n=======================\n 欢迎使用学生成绩管理系统 \n=======================");
        System.out.println("1. 记录学生成绩");
        System.out.println("2. 查询学生成绩");
        System.out.println("3. 统计课程成绩");
        System.out.println("4. 退出系统");
    }

    /**
     * Gets integer input from the user.
     *
     * @param prompt prompt message for the user
     * @return integer input by the user, or -1 if invalid
     */
    private static int getIntInput(String prompt) {
        System.out.print(prompt);
        try {
            return Integer.parseInt(scanner.nextLine());
        } catch (NumberFormatException e) {
            return -1; // Invalid input
        }
    }

    /**
     * Adds a new student record.
     */
    private static void addRecord() {
        System.out.println("===== 记录学生成绩 =====");
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();

        // Check for duplicate ID
        if (records.stream().anyMatch(r -> r.id.equals(id))) {
            System.out.println("Error: Student ID already exists.");
            return;
        }

        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        int score = getIntInput("请输入成绩（0-100）：");

        if (score < 0 || score > 100) {
            System.out.println("Error: Score must be between 0 and 100.");
            return;
        }

        records.add(new StudentRecord(name, id, course, score));
        System.out.println("成绩已成功记录！");
    }

    /**
     * Queries student records based on specified criteria.
     */
    private static void queryRecord() {
        System.out.println("===== 查询学生成绩 =====");
        System.out.print("""
            1. 按学生姓名查询
            2. 按学生学号查询
            3. 按课程名称查询
            """);
        int type = getIntInput("请输入选项序号：");
        if (type < 1 || type > 3) {
            System.out.println("Invalid query type.");
            return;
        }

        System.out.print("Enter search value: ");
        String value = scanner.nextLine();

        boolean found = false;
        for (StudentRecord r : records) {
            if ((type == 1 && r.name.equals(value)) ||
                    (type == 2 && r.id.equals(value)) ||
                    (type == 3 && r.course.equals(value))) {
                System.out.printf("姓名: %s, 学号: %s, 课程: %s, 成绩: %d%n",
                        r.name, r.id, r.course, r.score);
                found = true;
            }
        }
        if (!found) System.out.println("No records found.");
    }

    /**
     * Calculates statistics for course scores.
     */
    private static void calculateStats() {
        System.out.println("===== 统计课程成绩 =====");
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();

        List<Integer> scores = new ArrayList<>();
        for (StudentRecord r : records) {
            if (r.course.equals(course)) {
                scores.add(r.score);
            }
        }

        if (scores.isEmpty()) {
            System.out.println("No records found for this course.");
            return;
        }

        int sum = 0;
        double max = scores.get(0), min = scores.get(0);
        for (int score : scores) {
            sum += score;
            max = Math.max(max, score);
            min = Math.min(min, score);
        }
        double average = (double) sum / scores.size();

        System.out.printf("课程: %s%n平均分: %.2f%n最高分: %.1f%n最低分: %.1f%n",
                course, average, max, min);
    }
}
